package com.seres.math;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.seres.data.GameParameter;
import com.seres.data.StakeholderImpact;

public class StakeholderImpactCalculator {
	public List<StakeholderEthicalStatus> calcNewStakeholderStatus(List<StakeholderEthicalStatus> old, List<StakeholderImpact> choiceImpact){
		List<StakeholderEthicalStatus> newStatus = new ArrayList<StakeholderEthicalStatus>();
//		for(StakeholderEthicalStatus stakeholderEthicalStatus : old){
//			newStatus.add(stakeholderEthicalStatus.clone());
//		}
		
		Iterator<StakeholderEthicalStatus> it = old.iterator();
		while(it.hasNext()){
			StakeholderEthicalStatus status = it.next();
			float newStakeholderStatusValue = 0;
			
			Iterator<StakeholderImpact> it2 = choiceImpact.iterator();
			List<StakeholderImpact> impactsToRemove = new ArrayList<StakeholderImpact>();
			while(it2.hasNext()){
				StakeholderImpact impact = it2.next();
				if(impact.getStakeholder().equals(status.getStakeholder())){
					float factor;
					if(impact.getValue() >= 0){
						factor = ((status.getStakeholder().getMaximumStatusValue() - status.getValue())/status.getStakeholder().getMaximumStatusValue()) * GameParameter.getDempParameterSHStatus();
					}
					else{
						factor = ((status.getStakeholder().getMinimumStatusValue() - status.getValue())/status.getStakeholder().getMinimumStatusValue()) * GameParameter.getDempParameterSHStatus();

					}
					newStakeholderStatusValue = status.getValue() + impact.getValue() * factor;
					newStatus.add(new StakeholderEthicalStatus(newStakeholderStatusValue, status.getStakeholder()));
					
					impactsToRemove.add(impact);
				}
			}
			choiceImpact.removeAll(impactsToRemove);
		}
		
		return newStatus;
	}
}
